package org.example.batchutils.datasource;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Map;

@Component
public class ApplicationStartup {

    @Autowired
    private DataSourceConfigService dataSourceConfigService;

    @Autowired
    private DynamicRoutingDataSource dynamicRoutingDataSource;

    @PostConstruct
    public void init() {
        // 启动时加载数据源配置
        Map<String, DataSource> dataSources = dataSourceConfigService.loadAllDataSources();
        // 这里可以将数据源注册到动态数据源管理器中

        dataSources.forEach((key, dataSource) -> {
            dynamicRoutingDataSource.addDataSource(key, dataSource);
        });

        System.out.println("Loaded " + dataSources.size() + " data sources");
    }
}